import time,random
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

service = Service("D:\\WPS Cloud Files\\msedgedriver.exe")
opt = Options()
opt.add_argument("--disable-blink-features=AutomationControlled")

browser = webdriver.Edge(service=service,options=opt)
browser.page_load_strategy = "eager"

browser.get('https://www.tianyabbs.com/forum.php?mod=forumdisplay&fid=5&filter=author&orderby=dateline')
time.sleep(2)

# //*[@id="moderate"]/li[1]/div/div[3]/div[1]/a[1]
# //*[@id="moderate"]/li[2]/div/div[3]/div[1]/a[1]

# //*[@id="thread_subject"]
locator = (By.XPATH,'//*[@id="moderate"]/li/div/div[3]/div[1]/a[1]')
WebDriverWait(browser,4).until(EC.presence_of_all_elements_located(locator))

next_pages = browser.find_elements(By.XPATH,'//*[@id="moderate"]/li/div/div[3]/div[1]/a')
for i in next_pages:
    i.click()
    time.sleep(random.randint(20,30)/10)
    browser.switch_to.window(browser.window_handles[-1])

    locator1 = (By.XPATH,'//*[@id="thread_subject"]')
    WebDriverWait(browser, 4).until(
        EC.presence_of_element_located(locator1))
    try:
        name = browser.find_element(By.XPATH,'//*[@id="thread_subject"]').text
        content = browser.find_element(By.XPATH,'//*[@id="postmessage_6033"]').text
    except:
        pass
    with open(f'D:\\wps\\success_novel\\{name}.txt',"w",encoding="utf-8") as sn:
        sn.write(content)


browser.close()
"""！！！！！！！今日主题是切换窗口！！！！！！！！！
窗口的句柄"""

""" 250725 今天气死了兄弟们，坚持吧！！！！ """




















